package com.atguigu.java.test1;

import org.junit.Test;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * 实现TCP的网络编程
 * 例子一：客户端发送信息给服务端。服务端把数据显示在控制台上
 *
 * @author shkstart
 * @create 2021-02-01 18:42
 */
public class TCPTest1 {
    @Test
    //客户端
    public void Clent() {
        Socket socket = null;
        OutputStream op = null;
        try {
            //1.创建一个 Socket对象   指明服务器端的ip和端口号
            InetAddress i1 = InetAddress.getByName("192.168.0.106");

            socket = new Socket(i1, 8988);

            //2.创建一个输出流，用于输出数据
            op = socket.getOutputStream();
            op.write("你好,我是客户端".getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (op != null)
                    //3.资源关闭
                    op.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (socket != null)
                    socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }


    }


    @Test
    //服务端
    public void server() {
        ServerSocket skt = null;
        Socket ap = null;
        InputStream ip = null;
        ByteArrayOutputStream bs = null;
        try {
            //1.创建服务端ServerSocket   指明自己的端口号
            skt = new ServerSocket(8988);
            //2，表示 服务端可以接收来自客户端的Socket
            ap = skt.accept();
            //3.创建输入流
            ip = ap.getInputStream();

            //不建议 因为可能会出现乱码
//        byte[] bytes=new byte[1024];
//        int len;
//        while ((len= ip.read(bytes))!=-1){
//            String s1=new String(bytes,0,len);
//            System.out.println(s1);
//        }

            //4.读取输入流中的数据
            //字节数组流
            bs = new ByteArrayOutputStream();
            byte[] by = new byte[10];
            int len;
            while ((len = ip.read(by)) != -1) {
                bs.write(by, 0, len);
            }
            System.out.println(bs.toString());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //5.关闭流
            try {
                if (bs != null)
                    bs.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (ip != null)
                    ip.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (ap != null)
                    ap.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (skt != null)
                    skt.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }
}
